﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PilotProject;
using PilotMVC.Models;

//namespace PilotMVC
//{
 namespace PilotMVC.Controllers
{
        public class ScheduleTestController : Controller
        {

            private PilotEntities1 db = new PilotEntities1();

            public ActionResult Index()
            {
                var tokens = new ScheduledTest();
                return View(db.TestTokens.Include("Test").ToList());
            }

            public ActionResult EditTestToken(Guid id)
            {
                TestToken selectedTestToken = db.TestTokens.Where(item => item.Id == id).Single();
                return View(selectedTestToken);
            }

            [HttpPost]
            public ActionResult EditTestToken(TestToken testToken)
            {
                if (testToken.TestDate == null || testToken.Examiner.JPA == null)
                {
                    if (testToken.TestDate == null)
                    {
                        ModelState.AddModelError("Test Date : ", "Please enter the test date");
                    }
                    if (testToken.Examiner.JPA == null)
                    {
                        ModelState.AddModelError("Examiner JPA : ", "Please enter examiner's JPA");
                    }
                    return View();
                }


                Examiner updatedExaminer = new Examiner();
                try
                {
                    updatedExaminer = db.People.OfType<Examiner>().Where(item => item.JPA == testToken.Examiner.JPA).Single();
                }
                catch (InvalidOperationException ex)
                {
                    ModelState.AddModelError("Examiner JPA : ", "Please enter the correct examiner's JPA");
                    return View();
                }

                TestToken updatedTestToken = db.TestTokens.Where(item => item.Id == testToken.Id).Single();
                updatedTestToken.TestDate = testToken.TestDate;
                updatedTestToken.Examiner = updatedExaminer;
                db.SaveChanges();
                return View("Index", db.TestTokens);
            }


            public ActionResult ExaminerList(string jpa, string firstName, string lastName)
            {
                var pilots = from candidate in db.People.OfType<Candidate>() select candidate;
                var candidateView = new ManageCandidateView();



                if (!string.IsNullOrEmpty(jpa) && !jpa.Equals(""))
                {
                    candidateView.Candidates = db.People.Include("Address").OfType<Candidate>().Where(e => e.JPA.Contains(jpa)).ToList();
                }
                if (!string.IsNullOrEmpty(firstName) && !firstName.Equals(""))
                {
                    candidateView.Candidates = db.People.Include("Address").OfType<Candidate>().Where(e => e.FirstName.ToLower().Contains(firstName.ToLower())).ToList();
                }
                if (!string.IsNullOrEmpty(lastName) && !lastName.Equals(""))
                {
                    candidateView.Candidates = db.People.Include("Address").OfType<Candidate>().Where(e => e.LastName.ToLower().Contains(lastName.ToLower())).ToList();
                }
                return View(candidateView);
            }

            public ActionResult AddCandidate()
            {
                Candidate candidate = new Candidate();
                candidate.DateOfBirth = DateTime.Now; 
                return View(candidate);
            }

            //
            // POST: /Examiner/Create

            [HttpPost]
            public ActionResult AddCandidate(Candidate candidate, FormCollection formCollection)
            {
                if (ModelState.IsValid)
                {
                    var address = new Address();
                    address.Id = Guid.NewGuid();
                    address.SuburbId = candidate.Address.SuburbId;
                    address.Street1 = candidate.Address.Street1;
                    address.Street2 = candidate.Address.Street2;
                    db.Addresses.AddObject(address);
   

                    var person = new Candidate();
                    person.Id = Guid.NewGuid();
                    person.DateOfBirth = candidate.DateOfBirth;
                    person.Address = address;
                    person.ARN = candidate.ARN;
                    person.Email = candidate.Email;
                    person.FirstName = candidate.FirstName;
                    person.JPA = candidate.JPA;
                    person.LastName = candidate.LastName;
                    person.Telephone = candidate.Telephone;
                    person.Mobile = candidate.Mobile; 
                    db.People.AddObject(person);                
                    db.SaveChanges();
       
                   var maangeCandidate = new ManageCandidateView();

                   return View("ExaminerList", maangeCandidate);
                
                 }
                return View(candidate);
            }

            public ActionResult ConfirmationPage(Guid id)
            {
                Candidate candidate = db.People.OfType<Candidate>().Include("Address").Single(c => c.Id == id);
                return View(candidate);
            }


            [HttpPost]
            public ActionResult ConfirmationPage(Candidate candidate)
            {             
                ModelState.Remove("States");
                if (ModelState.IsValid)
                {
                    var person = db.People.Where(p => p.Id == candidate.Id).First();
                    person.Address.SuburbId = candidate.Address.SuburbId;
                    person.Address.Street1 = candidate.Address.Street1;
                    person.Address.Street2 = candidate.Address.Street2;
                    person.Email = candidate.Email;
                    person.FirstName = candidate.FirstName;
                    person.Mobile = candidate.Mobile;
                    person.Telephone = candidate.Telephone;
                    person.JPA = candidate.JPA;
                    person.ARN = candidate.ARN;
                    person.DateOfBirth = candidate.DateOfBirth;
                    person.LastName = candidate.LastName;
                    db.SaveChanges();
                    return RedirectToAction("CreateTestToken", candidate);
                }
                ViewBag.AddressId = new SelectList(db.Addresses, "Id", "Street1", candidate.AddressId);
                return View(candidate);
            }

            public ActionResult CreateTestToken(Candidate candidate)
            {
                var testToken = new TestToken();
                testToken.AddCandidate(candidate);
                return View(testToken);
            }

            [HttpPost]
            public ActionResult CreateTestToken(string buttonName, TestToken tokenTest)
            {
                if (buttonName.Equals("Cancel"))
                {
                    return View("Index", db.TestTokens);
                }

               

                if (tokenTest.TestDate == null || tokenTest.Examiner.JPA == null)
                {
                    tokenTest.Candidate = db.People.OfType<Candidate>().Where(item => item.Id == tokenTest.Candidate.Id).Single();

                    if (tokenTest.TestDate == null)
                    {
                        ModelState.AddModelError("Select Test Date : ", "Please enter the test date");
                    }
                    if (tokenTest.Examiner.JPA == null)
                    {
                        ModelState.AddModelError("Set Examiner's JPA : ", "Please enter examiner's JPA");
                    }
                    return View(tokenTest);
                }


                Candidate selectedCandidate = db.People.OfType<Candidate>().Where(item => item.Id == tokenTest.Candidate.Id).Single();
                Examiner selectedExaminer = new Examiner();
                try
                {
                    selectedExaminer = db.People.OfType<Examiner>().Where(item => item.JPA == tokenTest.Examiner.JPA).Single();
                }
                catch (InvalidOperationException)
                {
                    ModelState.AddModelError("Set Examiner's JPA : ", "Please enter the correct examiner's JPA");
                    tokenTest.Candidate = selectedCandidate;
                    return View(tokenTest);
                }

                var pilotTestEngine = db.TestEngines.OfType<JumpPilotTestEngine>().First();
                var token = pilotTestEngine.GenerateTestToken(selectedCandidate, selectedExaminer);
                token.TestDate = tokenTest.TestDate;
                db.SaveChanges();
                token.selectedTestDate = token.TestDate.Value;
                return View("TestTokenDetails", token);
            }

            public ActionResult TestTokenDetails(TestToken token)
            {
                return View(token);
            }

            protected override void Dispose(bool disposing)
            {
                base.Dispose(disposing);
                db.Dispose();
            }

            public string JPA { get; set; }
        }


    }

